/**
 * 
 */
package com.shiplanet.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.shiplanet.dao.PhotoDAO;
import com.shiplanet.dao.PostDAO;
import com.shiplanet.entity.Photo;
import com.shiplanet.entity.Post;
import com.shiplanet.service.PostService;
import com.shiplanet.util.Constants;

/**
 * @author joesong
 *
 */
@Service("postService")
public class PostServiceImpl implements PostService {
	@Resource
	private PostDAO postDAO;

	@Resource
	private PhotoDAO photoDAO;

	public Integer savePost(Post post) {
		return (Integer) postDAO.save(post);
	}	
	
	public Integer savePostWithPhoto(Post post, File[] photoFiles,
			String realRootPath) throws Exception {
		
		createPhotoFiles(post, photoFiles, realRootPath);
		
		return (Integer) postDAO.save(post);
	}

	public void updatePostWithPhoto(Post originalPost, Post newPost,
			File[] photoFiles, String realRootPath) throws Exception {
		
		originalPost.setApproved(newPost.isApproved());
		originalPost.setTitle(newPost.getTitle());
		originalPost.setFeatured(newPost.isFeatured());

		originalPost.setEquipmentName(newPost.getEquipmentName());
		originalPost.setEquipmentItemNumber(newPost.getEquipmentItemNumber());
		originalPost.setEquipmentManufacturer(newPost
				.getEquipmentManufacturer());
		originalPost.setEquipmentLocation(newPost.getEquipmentLocation());
		originalPost
				.setEquipmentDescription(newPost.getEquipmentDescription());
		originalPost.setEquipmentIsSold(newPost.isEquipmentIsSold());
		originalPost.setEquipmentYear(newPost.getEquipmentYear());

		originalPost.setInterMemo(newPost.getInterMemo());
		
		// delete photo files for this post
		if (photoFiles != null && originalPost.getPhotos() != null) {
			Iterator<Photo> it = originalPost.getPhotos().iterator();
			while (it.hasNext()) {
				Photo photo = it.next();
				File photoFile = new File(realRootPath + photo.getURL());
				photoFile.delete();
				photo.setPost(null);
				it.remove();
			}
		}
		
		// add new photo files for this post
		createPhotoFiles(originalPost, photoFiles, realRootPath);

		postDAO.update(originalPost);
	}

	public void addPostPhotoFiles(Post post, File[] photoFiles,
			String realRootPath) throws Exception {

		createPhotoFiles(post, photoFiles, realRootPath);
		
		postDAO.update(post);
	}

	private void createPhotoFiles(Post post, File[] photoFiles, String realRootPath) throws Exception{
		Photo photo = null;
		
		if (photoFiles != null) {
			SimpleDateFormat formatter = new SimpleDateFormat(Constants.DATE_FORMAT);
			String date = formatter.format(new Date());
			
			for (File photoFile : photoFiles) {
				// generate a uuid for each photo file
				String uuid = UUID.randomUUID().toString();
				char[] dimensionArray = new char[] { 'L', 'M', 'S' };
				for (char d : dimensionArray) {
					String url = photoDAO.createPhotoFile(photoFile, date, uuid, d,
							realRootPath);
					photo = new Photo();
					photo.setDimension(d);
					photo.setURL(url);

					post.addPhoto(photo);
				}
			}
		}		
	}
	
	public void deletePost(Post post, String realRootPath) {
		
		// delete photo files for this post
		if (post.getPhotos() != null) {
			Iterator<Photo> it = post.getPhotos().iterator();
			while (it.hasNext()) {
				Photo photo = it.next();
				File photoFile = new File(realRootPath + photo.getURL());
				photoFile.delete();
				photo.setPost(null);
				it.remove();
			}
		}		
		
		postDAO.delete(post);
	}

	public Post getPost(Integer postId) {
		return postDAO.get(Post.class, postId);
	}

	public long countPost() {
		return postDAO.countPost();
	}

	public List<Post> findPost(int page, int rows) {
		return postDAO.findPost(page, rows);
	}

	public List<Post> findFeaturedPost() {
		return postDAO.findFeaturedPost();
	}
}
